//
//  TravelyoujiCollectionViewCell.m
//  ToWhere
//
//  Created by dllo on 15/11/19.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelyoujiCollectionViewCell.h"
#import "EnterTableViewCell.h"
#import "EntertainmentModel.h"
#import <MJRefresh.h>
#import "DataAnalysis.h"
@interface TravelyoujiCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, retain) MJRefreshNormalHeader *header;

@property (nonatomic, retain) MJRefreshAutoGifFooter *footer;

@property (nonatomic, assign) NSInteger DataNumber;

@property (nonatomic, retain) UIAlertController *al;
@end
@implementation TravelyoujiCollectionViewCell
- (void)dealloc
{
    [_tableView release];
    [_array release];
    [_header release];
    [_footer release];
    [_al release];
    [_searchName release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        //解析数据
//        [self gainData];
        //初始化
        [self initTableView];
        
        self.array = [NSMutableArray array];
        
        self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(Refresh)];
        
        self.header.lastUpdatedTimeLabel.hidden = YES;
        
        self.tableView.header = self.header;
        self.footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        
        [self.footer setTitle:@"Loading more ..." forState:MJRefreshStateRefreshing];
        
        self.tableView.footer = self.footer;
        
    }
    return self;
}
#pragma init 
- (void)initTableView{

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375*kWidth, 567 *kHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[EnterTableViewCell class] forCellReuseIdentifier:@"enterTableViewCell"];
    [self.contentView addSubview:_tableView];
    [_tableView release];
}
#pragma mark 刷新
- (void)Refresh{
    
    [self.array removeAllObjects];
    _DataNumber = 1;
    [self.tableView reloadData];
    [self gainData];
    
}
- (void)loadMoreData{
    
    
    _DataNumber += 1;
    
    if(_DataNumber <= 5){
        
        [self gainData];
    }
    else
        [self.footer noticeNoMoreData];
}
#pragma tableView
- (void)setSearchName:(NSString *)searchName{

    if (_searchName != searchName) {
        [_searchName release];
        _searchName = [searchName copy];
        [self.tableView.header beginRefreshing];
    }
}
#pragma mark 解析数据
- (void)gainData{

    NSString *urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/search/trips.json?q=%@&page=%ld",self.searchName,_DataNumber];
    urlString =  [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [DataAnalysis dataAnalysisGETWithUrlString:urlString parameters:self showHuD:YES hiddenView:nil successfulBlock:^(id responseObject) {
        
        NSArray *data = responseObject;
        if (data.count == 0) {
            
            [self.delegate senderPer];
            return ;
        }
        for (NSDictionary *dic in data) {
            
            EntertainmentModel *enter = [EntertainmentModel enterWithDictionary:dic];
            [self.array addObject:enter];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    } failureBlock:^(NSError *error) {
        
    }];
}
#pragma mark tableView代理回调方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    EnterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"enterTableViewCell" forIndexPath:indexPath];
    cell.enterModel = self.array[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 200 *kHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.delegate send:self.array[indexPath.row]];
}
@end
